package com.hhl.flower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhl.flower.mapper.AddressMapper;
import com.hhl.flower.pojo.entity.Address;
import com.hhl.flower.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Liubuzhu
* @description 针对表【address】的数据库操作Service实现
* @createDate 2024-06-12 14:38:37
*/
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address>
    implements AddressService{
	@Autowired
	private AddressMapper addressMapper;

	public List<Address> getAddressByUser(Integer userId) {
		QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("u_id", userId);
		queryWrapper.orderByDesc("is_default"); // 根据 is_default 字段按降序排序
		return addressMapper.selectList(queryWrapper);
	}

	public void addAddress(Integer userId, String consignee, String phone, String detail, Integer isDefault) {
		// 假如这里要设置默认地址，要修改原来的默认地址
		if (isDefault == 1) {
			QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("u_id",userId);
			queryWrapper.eq("is_default",1);
			Address address = addressMapper.selectOne(queryWrapper);
			address.setIsDefault(0);
			addressMapper.updateById(address);
		}
		Address address = new Address();
		address.setUId(userId);
		address.setConsignee(consignee);
		address.setPhone(phone);
		address.setDetail(detail);
		address.setIsDefault(isDefault);

		addressMapper.insert(address);
	}

	public void deleteAddress(Integer userId, Integer addressId) {
		addressMapper.deleteById(addressId);
	}

}




